﻿#include <QMessageBox>
#include <QMenu>
#include "PageLawBasis.h"
#include "ui_PageLawBasis.h"
#include "../DatabaseMgmt/AppraisalBasis.h"
#include "../DatabaseMgmt/AccordLaw.h"
#include "GlobalData.h"

CPageLawBasis::CPageLawBasis(QWidget* parent /*= 0*/, Qt::WindowFlags f /*= 0*/)
	: QWidget(parent, f)
{
	init();
}

CPageLawBasis::~CPageLawBasis()
{

}

void CPageLawBasis::openTestReport()
{
	uint32_t nTestReportID = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportID == 0)
	{
		return;
	}
	clearChecked();

	QVector<AccordLaw> accords;
	CAccordLaw law;
	if (law.queryByReportUid(nTestReportID, accords) != CONST_ERR_SUCCESS)
	{
		return;
	}
	
	for (int i = 0; i < accords.count(); ++i)
	{
		for (int j = 0; j < m_ui->listAppraisal->count(); ++j)
		{
			QListWidgetItem* pItem = m_ui->listAppraisal->item(j);
			if (pItem->checkState() == Qt::Checked)
			{
				continue;
			}
			else if (pItem->data(Qt::UserRole).toUInt() == accords.at(i).appraisal_uid)
			{
				pItem->setCheckState(Qt::Checked);
			}
		}
	}
}

void CPageLawBasis::savePage()
{
	slotSaveLaw();
}

void CPageLawBasis::clear()
{
	m_ui->leSearchText->clear();
	m_ui->leAppraisalName->clear();
}

void CPageLawBasis::init()
{
	m_ui = new Ui::PageLawBasis();
	m_ui->setupUi(this);


	initLawesList();

	connect(m_ui->btnAppraisal, SIGNAL(clicked()), this, SLOT(slotAddAppraisal()));
	connect(m_ui->btnRefresh, SIGNAL(clicked()), this, SLOT(slotRefreshLaw()));
	connect(m_ui->btnSave, SIGNAL(clicked()), this, SLOT(slotSaveLaw()));
	connect(m_ui->btnSearch, SIGNAL(clicked()), this, SLOT(slotSearchLaw()));

// 	connect(m_ui->listAppraisal, SIGNAL(customContextMenuRequested(const QPoint&)),
// 		this, SLOT(slotAppraisalRightMenu(const QPoint&)));

}

void CPageLawBasis::slotAddAppraisal()
{
	QString strText = m_ui->leAppraisalName->text();
	if (strText.trimmed().isEmpty())
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("法律或规定依据不能为空！"));
		return;
	}
	CAppraisalBasis basis;
	int ret = basis.insertRow(strText);

	if (ret != CONST_ERR_SUCCESS)
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("添加法律规定失败！"));
		return;
	}
	initLawesList();
}

void CPageLawBasis::slotAppraisalRightMenu(const QPoint& pos)
{
	Q_UNUSED(pos);
	QMenu menu(this);

	menu.addAction(QStringLiteral("删除"), this, SLOT(slotDelAppraisalTriggered(bool)));

	menu.exec(QCursor::pos());
}

void CPageLawBasis::initLawesList()
{
	CAppraisalBasis appraisal;

	QVector<LawBasis> lawes;
	int ret = appraisal.queryAll(lawes);
	if (CONST_ERR_SUCCESS != ret)
	{
		return;
	}

	m_ui->listAppraisal->clear();

	for (int i = 0; i < lawes.size(); ++i)
	{
		QListWidgetItem* pItem = new QListWidgetItem(lawes[i].law);
		pItem->setData(Qt::UserRole, lawes[i].uid);
		pItem->setCheckState(Qt::Checked);

		m_ui->listAppraisal->addItem(pItem);
	}
}

void CPageLawBasis::clearChecked()
{
	for (int i = 0; i < m_ui->listAppraisal->count(); ++i)
	{
		QListWidgetItem* pItem = m_ui->listAppraisal->item(i);
		pItem->setCheckState(Qt::Unchecked);
	}
}

void CPageLawBasis::slotDelAppraisalTriggered(bool checked /*= false*/)
{
	Q_UNUSED(checked);
	QListWidgetItem* pItem = m_ui->listAppraisal->currentItem();
	if (nullptr == pItem)
	{
		return;
	}
	uint32_t uid = pItem->data(Qt::UserRole).toUInt();

	CAppraisalBasis basis;
	int ret = basis.removeRow(uid);
	if (ret != CONST_ERR_SUCCESS)
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("删除法律规定失败！"));
	}
	else
	{
		initLawesList();
	}
}

void CPageLawBasis::slotRefreshLaw()
{
	initLawesList();
	// 重新打开当前的报告
	openTestReport();
}

void CPageLawBasis::slotSaveLaw()
{
	uint32_t nTestReportID = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportID == 0)
	{
		return;
	}
	CAccordLaw accord;
	accord.removeByReportUid(nTestReportID);

	for (int i = 0; i < m_ui->listAppraisal->count(); ++i)
	{
		QListWidgetItem* pItem = m_ui->listAppraisal->item(i);
		if (pItem->checkState() == Qt::Unchecked)
		{
			continue;
		}
		
		AccordLaw law;
		law.report_uid = nTestReportID;
		law.appraisal_uid = pItem->data(Qt::UserRole).toUInt();
		accord.insertRow(law);
	}
}

void CPageLawBasis::slotSearchLaw()
{
	QString strText = m_ui->leSearchText->text().trimmed();
	if (strText.isEmpty())
	{
		for (int i = 0; i < m_ui->listAppraisal->count(); ++i)
		{
			QListWidgetItem* pItem = m_ui->listAppraisal->item(i);
			pItem->setHidden(false);
		}
	}
	else
	{
		for (int i = 0; i < m_ui->listAppraisal->count(); ++i)
		{
			QListWidgetItem* pItem = m_ui->listAppraisal->item(i);
			if (pItem->text().contains(strText))
			{
				pItem->setHidden(false);
			}
			else
			{
				pItem->setHidden(true);
			}
		}
	}
}
